'''
本程序将实现，小猿口算的自动化处理
'''
import cv2
import time
import pyautogui
import numpy as np

'''
以下是本程序的实现逻辑：

首先该程序会利用鼠标来确定，三个窗口。
1窗口是用来判断题目是否变化的，0.5秒执行一次
2，3窗口都是用来截取比赛过程中的数字。
得到坐标后，如果判断题目没有变化，则不处理，有变化这开始执行操作
在操作前，我们需要先裁剪20张数字图片来作为模板进行匹配。
一旦匹配成功会返回模板存储的数字下标，该下标是对应数字的
最后就是比较大小，如何操作鼠标进大小写的输入
'''
def isSamePhoto(image1,image2,threshold=10):
    # 转化为灰度图，为了后续图片比较
    print('正在转化灰度图')
    num_channels1 = len(image1.shape)
    num_channels2 = len(image2.shape)
    if num_channels1 == 3:
        gray1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
    else :
        gray1 = image1
    if num_channels2 == 3:
        gray2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
    else :
        gray2 = image2
    result = cv2.subtract(gray1,gray2)
    non_zero_count = cv2.countNonZero(result)
    return non_zero_count<=threshold
def catchNum(template,imagecat):
    '''
    for i in range(0,len(template)):
        if isSamePhoto(template[i],imagecat):
            return i
        else:
            pass
    return -1
    '''

    '''
    利用模板匹配来找到局部相同的坐标，利用坐标和模板图片大小来切分图片
    最后进行比较
    '''
    gray1 = cv2.cvtColor(imagecat,cv2.COLOR_BGR2GRAY)
    for i in range(0,len(template)):
        print(f'开始i = {i}的比较')
        gray2 = cv2.cvtColor(template[i],cv2.COLOR_BGR2GRAY)
        print('读取模板的宽度和高度')
        height,width = gray2.shape
        print('进行模板匹配')
        result = cv2.matchTemplate(gray1,gray2,cv2.TM_SQDIFF_NORMED)
        print('解析出匹配区域的左上角坐标')
        upper_left = cv2.minMaxLoc(result)[2]
        print('计算右下角坐标')
        lower_right = (upper_left[0]+width,upper_left[1]+height)
        print('截取图片')
        imageSameTem = gray1[upper_left[1]:lower_right[1],upper_left[0]:lower_right[0]]
        if isSamePhoto(imageSameTem,gray2):
            return i
        


            '''
            match = cv2.matchTemplate(gray1,gray2,cv2.TM_CCOEFF_NORMED)
            locations = np.where(match>=0.9)
            w,h = gray2.shape[0:2]
            for p in zip(*locations[::-1]):
                x1,y1 = p[0],p[1]
                x2,y2 = x1+w,y1+h
                imageZip = gray1[y1:y2,x1:x2]
                if imageZip is not None:
                    pass
                else:
                    print("图像加载失败")
                if isSamePhoto(imageZip,gray2):
                    return i
            '''
    return -1

def drawLessThanSign(x,y):
    #获取鼠标的位置
    print('鼠标左键开始点击')
    pyautogui.mouseDown(button='left')
    print('开始移动鼠标')
    pyautogui.moveTo(x-10,y+10,duration=0.3)
    pyautogui.moveTo(x,y+20,duration=0.3)
    print('鼠标左键松开')
    pyautogui.mouseUp(button='left')

def drawMoreThanSian(x,y):
    print('鼠标左键开始点击')
    pyautogui.mouseDown(button='left')
    print('开始移动鼠标')
    pyautogui.moveTo(x+10,y+10,duration=0.3)
    pyautogui.moveTo(x,y+20,duration=0.3)
    print('鼠标左键松开')
    pyautogui.mouseUp(button='left')
def drawSameSian(x,y):

    print('鼠标左键开始点击')
    pyautogui.mouseDown(button='left')
    print('开始移动鼠标')
    pyautogui.moveTo(x+10,y,duration=0.3)
    print('鼠标左键松开')
    pyautogui.mouseUp(button='left')
    
    pyautogui.moveTo(x,y+10,duration=0)
    print('鼠标左键开始点击')
    pyautogui.mouseDown(button='left')
    print('鼠标左键松开')
    pyautogui.mouseUp(button='left')



#创建一个存储各个模板路径的字符串列表
print('录入模板路径中~')
strlist = ['D:/code/python/Template/0.png',
           'D:/code/python/Template/1.png',
           'D:/code/python/Template/2.png',
           'D:/code/python/Template/3.png',
           'D:/code/python/Template/4.png',
           'D:/code/python/Template/5.png']
#存储各个模板，为了后续匹配且数组下标与内容匹配。

template = list()
print('录入模板中')
for str in strlist:
    imageTem = cv2.imread(str)
    #gray = cv2.cvtColor(imageTem,cv2.COLOR_BGR2GRAY)
    template.append(imageTem)

print('开始获取第一个窗口的左上角坐标')
time.sleep(3)
windowsFirstx1,windowsFirsty1 = pyautogui.position()
print('开始获取第一个窗口的右下角坐标')
time.sleep(3)
windowsFirstx2,windowsFirsty2 = pyautogui.position()

print('开始获取第二个窗口的左上角坐标')
time.sleep(3)
windowsSecondx1,windowsSecondy1 = pyautogui.position()
print('开始获取第二个窗口的右下角坐标')
time.sleep(3)
windowsSecondx2,windowsSecondy2 = pyautogui.position()
print('开始获取第三个窗口的左上角坐标')
time.sleep(3)
windowsThirdx1,windowsThirdy1 = pyautogui.position()
print('开始获取第三个窗口的右下角坐标')
time.sleep(3)
windowsThirdx2,windowsThirdy2 = pyautogui.position()
print('开始获取鼠标绘画区域')
time.sleep(3)
mouseMovex,mouseMovey =  pyautogui.position()
print('坐标获取结束')

#获取第一个窗口信息
screenshot1 = pyautogui.screenshot(region=(windowsFirstx1,windowsFirsty1,windowsFirstx2-windowsFirstx1,windowsFirsty2-windowsFirsty1))
screenshot1.save('screenshot1.png')
image = cv2.imread('screenshot1.png')
#cv2.imshow('image',image)
while True:
    if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    print('开始获取第一个窗口')
    screenshot1Tmp = pyautogui.screenshot(region=(windowsFirstx1,windowsFirsty1,windowsFirstx2-windowsFirstx1,windowsFirsty2-windowsFirsty1))
    print('开始存储第一个窗口')
    screenshot1Tmp.save('screenshot1Tmp.png')
    print('正在打开第1个窗口')
    imageTmp = cv2.imread('screenshot1Tmp.png')
    #cv2.imshow('imageTmp',imageTmp)
    time.sleep(0.2)
    if isSamePhoto(image,imageTmp):
        print('图片相同！')
    else:
        print('图片不同，开始执行操作')
        #窗口更新，开始执行目标操作
        image,imageTmp = imageTmp,image
        '''
        cv2.imshow('image',image)
        cv2.imshow('imageTmp',imageTmp)
        '''
        screenshot2 = pyautogui.screenshot(region=(windowsSecondx1,windowsSecondy1,windowsSecondx2-windowsSecondx1,windowsSecondy2-windowsSecondy1))
        screenshot2.save('screenshot2.png')
        image2 = cv2.imread('screenshot2.png')
        screenshot3 = pyautogui.screenshot(region=(windowsThirdx1,windowsThirdy1,windowsThirdx2-windowsThirdx1,windowsThirdy2-windowsThirdy1))
        screenshot3.save('screenshot3.png')
        image3 = cv2.imread('screenshot3.png')
        left = catchNum(template,image2)
        print(f'left = {left}')
        right = catchNum(template,image3)
        print(f'right = {right}')

        if left == -1 or right == -1:
            exit
        if left<right:
            drawLessThanSign(mouseMovex,mouseMovey)
        elif left>right:
            drawMoreThanSian(mouseMovex,mouseMovey)
        else:
            drawSameSian(mouseMovex,mouseMovey)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
            
        '''
        compare1 = catchNum(template,image2)
        compare2 = catchNum(template,image3)
        if compare1>compare2:
            drawMoreThanSian()
        elif compare1 < compare2:
            drawLessThanSign()
        else:
            pass
        
        '''
        
